package com.babyqiao.wallpaper.manager.controller;


import com.babyqiao.wallpaper.common.UnifyResult;
import com.babyqiao.wallpaper.manager.controller.dto.AddAlbumParams;
import com.babyqiao.wallpaper.manager.controller.dto.AlbumQuery;
import com.babyqiao.wallpaper.manager.entity.WallpaperAlbum;
import com.babyqiao.wallpaper.manager.entity.vo.AlbumResVO;
import com.babyqiao.wallpaper.manager.service.AlbumService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author qinglongzhai
 */
@Api(tags = "专辑管理接口")
@RequestMapping("/api/admin/album")
@RestController
public class AlbumController {

    @Autowired
    private AlbumService albumService;

    /**
     * 获取所有专辑的方法
     */
    @GetMapping("/all")
    @ApiOperation(value = "查找所有专辑信息", httpMethod = "GET")
    public UnifyResult findAllAlbum() {
        List<AlbumResVO> allAlbumService = albumService.getAllAlbumService();
          return UnifyResult.success().data("result", allAlbumService);
    }
    /**
     * 根据ID查询专辑
     */
    @GetMapping("album/{id}")
    @ApiOperation(value = "根据ID查询专辑", httpMethod = "GET")

    public UnifyResult getAlbumById(@ApiParam(name = "id", value = "专辑ID", required = true) @PathVariable String id) {
        AlbumResVO allAlbumService = albumService.getAlbumbyIdService(id);
        //WallpaperAlbum wallpaperAlbum = albumService.getById(id);
        return UnifyResult.success().data("result", allAlbumService);
    }




    /**
     * 条件查询带分页
     */
    @ApiOperation(value = "多条件分页查询", httpMethod = "POST")
    @PostMapping("/pageAlbumCondition/{current}/{limit}")
    public UnifyResult pageAlubmCondition(@ApiParam(name = "current", value = "当前页码", required = true) @PathVariable long current,
                                          @ApiParam(name = "limit", value = "每页查询条数", required = true) @PathVariable long limit, @RequestBody(required = false) AlbumQuery albumQuery) {
        //创建page对象
        Page<AlbumResVO> page = new Page<>(current, limit);
        Page<AlbumResVO> queryWrapper = albumService.addSelectCondition(page,albumQuery);


        //返回共有多少条，此处返回共有多少条是不准确的，它返回的是sql语句一对多查询出来的条数
        //一对多查询会产生重复条数，而实际上我们直接将多通过mybatis封装成了list，故实际上返回的数据会少于条数
        long total = queryWrapper.getTotal();
        //数据的list
        List<AlbumResVO> pageRecords = queryWrapper.getRecords();

        return UnifyResult.success().data("total", total).data("result", pageRecords);

    }


    /**
     * 删除专辑
     */
    @ApiOperation(value = "逻辑删除专辑", httpMethod = "DELETE")
    @DeleteMapping("{id}")
    public UnifyResult removeAlbum(@ApiParam(name = "id", value = "专辑ID", required = true) @PathVariable String id) {
        boolean b= albumService.delAlbumService(id);

        return b?UnifyResult.success():UnifyResult.error();

    }

    /**
     * 切换专辑上下线
     */
    @ApiOperation(value = "切换专辑上下线", httpMethod = "GET")
    @GetMapping("online/{id}/{state}")
    public UnifyResult modifiyAlbumOnline(@ApiParam(name = "id", value = "专辑ID", required = true) @PathVariable String id,
                                          @ApiParam(name = "state", value = "上线状态（0=false 1=true", required = true) @PathVariable Integer state) {
        UpdateWrapper<WallpaperAlbum> updateWrapper = albumService.addUpdataCondition(id, state);
        boolean update = albumService.update(updateWrapper);
        if (update) {
            return UnifyResult.success().data("result", update);
        }
        return UnifyResult.error().data("result", update);


    }


    /**
     * 添加专辑
     */
    @ApiOperation(value = "添加专辑", httpMethod = "POST")
    @PostMapping("addAlbum")
    public UnifyResult addAlbum(@ApiParam(name = "album", value = "专辑对象", required = true, example = "{'albumName':'张三','online':'1'}") @RequestBody AddAlbumParams addAlbumParams) {
        System.out.println(addAlbumParams);
        boolean save=albumService.addAblumService(addAlbumParams);

       return save?UnifyResult.success():UnifyResult.error();
    }



    /**
     * 修改专辑
     */
    @PostMapping("updateAlbum")
    @ApiOperation(value = "修改专辑", httpMethod = "POST")
    public UnifyResult undataAlbum(@RequestBody AddAlbumParams addAlbumParams) {
       boolean b= albumService.updateAlbumService(addAlbumParams);
      return b ? UnifyResult.success() : UnifyResult.error();

    }


}
